﻿// -----------------------------------------------------------------------
// <copyright file="AdministrativeUnitData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITIES;
   
    /// <summary>
    /// Class access to db AdministrativeUnit
    /// </summary>
    public class AdministrativeUnitData
    {
        /// <summary>
        /// Get AdministrativeUnit By Id
        /// </summary>
        /// <param name="administrativeUnitId">Param administrativeUnitId</param>
        /// <returns>Return AdministrativeUnit</returns>
        public AdministrativeUnitEN GetAdministrativeUnitById(int administrativeUnitId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                AdministrativeUnitEN administrativeUnit = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneAdministrativeUnitGetById, administrativeUnitId);
                while (reader.Read())
                {
                    administrativeUnit = new AdministrativeUnitEN();

                    if (!reader.IsDBNull(0))
                    {
                        administrativeUnit.AdministrativeUnitsId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        administrativeUnit.Code = reader.GetString(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        administrativeUnit.Name = reader.GetString(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        administrativeUnit.Parent = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        administrativeUnit.Order = reader.GetInt32(4);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return administrativeUnit;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all AdministrativeUnit
        /// </summary>
        /// <returns>Return list all AdministrativeUnit</returns>
        public IList<AdministrativeUnitEN> GetAdministrativeUnits()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<AdministrativeUnitEN> lstAdministrativeUnit = new List<AdministrativeUnitEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneAdministrativeUnitGetAll);
                while (reader.Read())
                {
                    AdministrativeUnitEN administrativeUnit = new AdministrativeUnitEN();

                    if (!reader.IsDBNull(0))
                    {
                        administrativeUnit.AdministrativeUnitsId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        administrativeUnit.Code = reader.GetString(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        administrativeUnit.Name = reader.GetString(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        administrativeUnit.Parent = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        administrativeUnit.Order = reader.GetInt32(4);
                    }

                    lstAdministrativeUnit.Add(administrativeUnit);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstAdministrativeUnit;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db AdministrativeUnit
        /// </summary>
        /// <param name="administrativeUnit">Paarm AdministrativeUnit</param>
        /// <returns>Insert AdministrativeUnit</returns>
        public int Insert(AdministrativeUnitEN administrativeUnit)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.AdministrativeUnit.Code, administrativeUnit.Code),
                                    new SqlParameter(Constants.SqlParamerterName.AdministrativeUnit.Name, administrativeUnit.Name),
                                    new SqlParameter(Constants.SqlParamerterName.AdministrativeUnit.Parent, administrativeUnit.Parent),
                                    new SqlParameter(Constants.SqlParamerterName.AdministrativeUnit.Order, administrativeUnit.Order)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneAdministrativeUnitInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Update to db AdministrativeUnit
        /// </summary>
        /// <param name="administrativeUnit">Param AdministrativeUnit</param>
        public void Update(AdministrativeUnitEN administrativeUnit)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.AdministrativeUnit.AdministrativeUnitsId, administrativeUnit.AdministrativeUnitsId),
                                    new SqlParameter(Constants.SqlParamerterName.AdministrativeUnit.Code, administrativeUnit.Code),
                                    new SqlParameter(Constants.SqlParamerterName.AdministrativeUnit.Name, administrativeUnit.Name),
                                    new SqlParameter(Constants.SqlParamerterName.AdministrativeUnit.Parent, administrativeUnit.Parent),
                                    new SqlParameter(Constants.SqlParamerterName.AdministrativeUnit.Order, administrativeUnit.Order)
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneAdministrativeUnitUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete AdministrativeUnit by administrativeUnitId
        /// </summary>
        /// <param name="administrativeUnitId">param administrativeUnitId</param>
        public void Delete(int administrativeUnitId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneAdministrativeUnitDelete, administrativeUnitId);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get AdministrativeUnit By parentId
        /// </summary>
        /// <param name="parentId">Param parentId</param>
        /// <param name="level">Param level</param>
        /// <returns>Return List AdministrativeUnit</returns>
        public IList<AdministrativeUnitEN> GetAdministrativeUnitByParentId(int parentId, int level)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<AdministrativeUnitEN> lstAdministrativeUnit = new List<AdministrativeUnitEN>();
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneAdministrativeUnitByParentId, parentId, level);
                while (reader.Read())
                {
                    var administrativeUnit = new AdministrativeUnitEN();

                    if (!reader.IsDBNull(0))
                    {
                        administrativeUnit.AdministrativeUnitsId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        administrativeUnit.Code = reader.GetString(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        administrativeUnit.Name = reader.GetString(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        administrativeUnit.Parent = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        administrativeUnit.Order = reader.GetInt32(4);
                    }

                    lstAdministrativeUnit.Add(administrativeUnit);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstAdministrativeUnit;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Exist code
        /// </summary>
        /// <param name="administrativeUnitsId">Param administrativeUnitsId</param>
        /// <param name="code">Param code</param>
        /// <returns>Return Exist</returns>
        public bool Exist(int administrativeUnitsId, string code)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.AdministrativeUnit.AdministrativeUnitsId, administrativeUnitsId),
                    new SqlParameter(Constants.SqlParamerterName.AdministrativeUnit.Code, code)
                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneAdministrativeUnitExist, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result > 0;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}